﻿using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using KDS.Mes.DataExporting.Excel.EpPlus;
using KDS.Mes.Dto;
using KDS.Mes.Mes.Styles.Dtos;
using System.Collections.Generic;

namespace KDS.Mes.Mes.Styles
{
    /// <summary>
    /// 款式信息的导出EXCEL功能的实现
    /// </summary>
    public class StyleListExcelExporter : EpPlusExcelExporterBase, IStyleListExcelExporter
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;

        /// <summary>
        /// 构造方法
        /// </summary>
        public StyleListExcelExporter(ITimeZoneConverter timeZoneConverter, IAbpSession abpSession)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }

        /// <summary>
        /// 导出款式信息到EXCEL文件
        /// <param name="styleListDtos">导出信息的DTO</param>
        /// </summary>
        public FileDto ExportStyleToFile(List<StyleListDto> styleListDtos)
        {
            var file = CreateExcelPackage("styleList.xlsx", excelPackage =>
            {
                var sheet = excelPackage.Workbook.Worksheets.Add(L("Style"));
                sheet.OutLineApplyStyle = true;

                AddHeader(
                    sheet,
                      L("CartypeId"),
 L("StyleName"),
 L("LastModificationTime"),
 L("CreationTime")
                    );
                AddObjects(sheet, 2, styleListDtos,

             _ => _.CartypeId,

             _ => _.StyleName,

        _ => _timeZoneConverter.Convert(_.LastModificationTime, _abpSession.TenantId, _abpSession.GetUserId()),
        _ => _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
       );
                //写个时间转换的吧
                //var creationTimeColumn = sheet.Column(10);
                //creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                for (var i = 1; i <= 4; i++)
                {
                    sheet.Column(i).AutoFit();
                }
            });
            return file;
        }
    }
}